<template>
  <ele-modal
    form
    :width="1400"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
        row-key="id"
        row-click-checked
      >
        <template #toolbar="{}">
          <el-button
            type="primary"
            v-if="props.action === 'buy' || props.action === 'wating-confirm'"
            @click="toDone()"
          >
            设为已出单
          </el-button>
        </template>
        <template #rebate="{ row }">
          <el-input
            v-if="
              (props.action == 'buy' || props.action == 'wating-confirm') &&
              row.cate !== '交强险' &&
              row.cate !== '商业险'
            "
            v-model="row.rebate"
          />
          <span v-else>{{ row.rebate }}</span>
        </template>
        <template #insurance_salesman="{ row }">
          <span>{{ row.insurance_salesman }}</span>
        </template>
        <template #insurance="{ row }">
          {{ row.insurance }}
        </template>
        <template #price="{ row }">
          <span :class="priceClass(row.raise_price)">{{ row.price }}</span>
        </template>
        <template #s_time="{ row }">
          <span>{{ row.sTime }}</span>
        </template>
        <template #e_time="{ row }">
          <span>{{ row.eTime }}</span>
        </template>

        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
        <template #action="{ row }">
          <el-button type="primary" @click="openRefound(row)"> 退保 </el-button>
        </template>
      </ele-pro-table>
    </ele-card>

    <!-- 图片 -->
    <images v-model="showImages" :data="current" :read-only="true" />
  </ele-modal>
</template>

<script setup>
  import { ref, watch, computed } from 'vue';
  import { pageInsuranceOffer } from '@/api/company';
  const emit = defineEmits(['done', 'update:modelValue']);

  import Images from '@/views/insurance/components/images.vue';

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    data: Object,
    planId: {
      type: Number,
      default: 0
    },
    vehicleId: {
      type: Number,
      default: 0
    },

    offerType: {
      type: String,
      default: '全部'
    },

    action: String
  });
  const title = ref('');
  const current = ref([]);
  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: '编号'
      },
      {
        width: 100,
        prop: 'cate',
        label: '类型'
      },
      {
        width: 100,
        prop: 'plate_number',
        label: '车牌号'
      },
      {
        slot: 'insurance',
        label: '保险公司'
      },
      {
        width: 100,
        slot: 'insurance_salesman',
        label: '业务员'
      },
      {
        width: 80,
        slot: 'rebate',
        label: '返点'
      },
      {
        width: 120,
        slot: 'price',
        label: '价格(不含加价)'
      },
      {
        width: 100,
        prop: 'raise_price',
        label: '加价'
      },
      {
        width: 200,
        slot: 's_time',
        label: '开始日期'
      },
      {
        width: 200,
        slot: 'e_time',
        label: '结束日期'
      },
      {
        width: 150,
        prop: 'createDate',
        label: '报价日期'
      },
      {
        width: 100,
        slot: 'img',
        label: '报价图片',
        align: 'center'
      }
    );

    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 表格数据源 */
  var offerList = ref({});
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsuranceOffer({
      ...where,
      ...orders,
      page,
      limit,
      vehicle_id: props.data.vehicle_id
    });
    offerList.value = result.list;
    return result;
  };

  const priceClass = (value) => {
    return value > 0 ? 'red' : 'blue';
  };

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = props.data.plate_number + '的报价列表';
      } else {
        console.log('关闭');
      }
    }
  );
</script>
